package com.abe.config;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.abe.app.ManagerAPP;
import com.abe.mvp.model.bean.exercise.ExerciseGroupBean;
import com.abe.utils.JsonHelper;
import com.abe.utils.SPUtils;
import com.google.gson.reflect.TypeToken;

/** 登录用户锻炼报告 */
public class ExerciseReportUtil {

	private static String ExerciseReport = "ExerciseReport";
	private static String ExerciseReportEmpty = JsonHelper
			.toJson(new ArrayList<ExerciseGroupBean>());

	private static List<ExerciseGroupBean> getBeans() {
		String exerciseData = (String) SPUtils.get(ManagerAPP.getContext(),
				ExerciseReport, ExerciseReportEmpty);
		Type type = new TypeToken<List<ExerciseGroupBean>>() {
		}.getType();
		List<ExerciseGroupBean> groupBeans = JsonHelper.parseObject(
				exerciseData, type);
		return groupBeans;
	}

	/** 保存登录用户此次锻炼数据 */
	public static void saveExercise(ExerciseGroupBean bean) {
		List<ExerciseGroupBean> groupBeans = getBeans();
		groupBeans.add(bean);
		SPUtils.put(ManagerAPP.getContext(), ExerciseReport,
				JsonHelper.toJson(groupBeans));
	}

	/** 清除此次登录用户锻炼数据 */
	public static void clearExercise() {
		SPUtils.put(ManagerAPP.getContext(), ExerciseReport,
				ExerciseReportEmpty);
	}

	/** 获取本次登录累计锻炼数据 */
	public static ExerciseReportBean getExerciseReport() {
		int num;
		int keepTime = 0;
		float calorie = 0;
		ExerciseReportBean reportBean = ExerciseReportBean.getReport();
		List<ExerciseGroupBean> groupBeans = getBeans();
		num = groupBeans.size();
		for (int i = 0; i < num; i++) {
			ExerciseGroupBean bean = groupBeans.get(i);
			keepTime = keepTime + bean.getKeepTime();
			calorie = calorie + bean.getCalorie();
		}
		reportBean.setNum(num);
		reportBean.setKeepTime(keepTime);
		reportBean.setCalorie(calorie);
		return reportBean;
	}

	/** 获取当前登录用户锻炼数据 */

	public static class ExerciseReportBean {
		private int num;
		private int keepTime;
		private float calorie;

		public static ExerciseReportBean getReport() {
			return new ExerciseReportBean();
		}

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		public int getKeepTime() {
			return keepTime;
		}

		public void setKeepTime(int keepTime) {
			this.keepTime = keepTime;
		}

		public float getCalorie() {
			return calorie;
		}

		public void setCalorie(float calorie) {
			this.calorie = calorie;
		}

	}
}
